package edu.wpi.cs509.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import edu.wpi.cs509.representation.TeamColor;

/**
 * The GraveyardPlayerName panel is located at the top of the graveyard,
 * and is used to disply the name of the player.
 * 
 * When a graveyard is first created, the client application should set 
 * the name to something like "Waiting for player..." to indicate the 
 * player name is not yet known.  Once the player joins, the name can be 
 * updated and will automatically be repainted.
 * 
 * @author afoltan
 *
 */
@SuppressWarnings("serial")
public class GraveyardPlayerName extends JPanel {

	private static final Color blueTeamColor = Color.BLUE;
	private static final Color redTeamColor = Color.RED;
	private static final Color blinkColor = Color.ORANGE;
	private static final int msBlinkDelay = 100;
	
	// the player name represented as a label
	private JLabel textField;
	
	// the player color, used to select the correct paint color
	private Color graveyardColor;
	
	// the textfield blinks to indicate my turn
	// currentTextfieldColor contains the current color
	private Color currentTextfieldColor;
	
	// turn indicator
	private Timer blinkTimer;
	
	/**
	 * Constructor for a GraveyardPlayerName	
	 * @param TeamColor - the color of this player
	 * @param name - the string name for display
	 */
	public GraveyardPlayerName( TeamColor teamColor, String name ) {
				
		// initialize the label properties
		textField = new JLabel( "0000" );
		textField.setHorizontalAlignment(JTextField.CENTER);

		textField.setFont(new Font("Default", Font.BOLD, 16));

		if (teamColor == TeamColor.BLUE) {
			textField.setForeground( Color.WHITE );
			graveyardColor = blueTeamColor;
		} else {
			graveyardColor = redTeamColor;
		}

		setBackground( currentTextfieldColor );

		Dimension d =  textField.getPreferredSize();
		
		// make this label fixed dimension - always!
		textField.setMinimumSize( d );
		textField.setMaximumSize( d );

		
		// add the label to the panel
		add( textField, BorderLayout.CENTER );
		
		// set the label string
		setPlayerName( name );

		// show it
		textField.setVisible(true);
	}

	/**
	 * Sets the string name of the player for display
	 * @param name - the string to display
	 */
	public void setPlayerName(String name) {
		textField.setText( "  " + name + "  " );
	}

	/**
	 * Called to redraw the panel
	 * @param g - the graphics object used to redraw the panel
	 */
	public void paintComponent(Graphics g) {
		/*
		Insets inset = getInsets();
		
		int w = getWidth() - inset.left - inset.right;
		int h = getHeight() - inset.top - inset.bottom;
		
		if (color == TeamColor.BLUE) {
			g.setColor( bgColor );
		} else {
			g.setColor( red );
		}
		
		g.fillRect( inset.left, inset.top, w, h );
		*/

		textField.setBackground( currentTextfieldColor );
		textField.setOpaque(true);

	}

	/**
	 * Blink will hightlight and blink the background of the player
	 * name in the graveyard.  It is intended to be used to indicate 
	 * that is is the players turn.
	 * 
	 * @param blinkCount - number of times to toggle the background
	 */
	public void setMyTurn( int blinkCount ) {
		
		if (blinkTimer == null) {
			// if enabling, highlight now
			currentTextfieldColor = blinkColor;
			repaint();
				
			// start toggle timer
			blinkTimer = new Timer();
			blinkTimer.schedule( new BlinkHandler(blinkTimer, blinkCount), 0, msBlinkDelay );
		}
	}
	
	/**
	 * Restores the graveyard username to "not my turn" 
	 */
	public void clearMyTurn() {
		if (blinkTimer != null) {
			blinkTimer.cancel();
			blinkTimer = null;
		}
		currentTextfieldColor = graveyardColor;
		repaint();
	}

	/**
	 * TimerTask to blink the graveyard username
	 * @author afoltan
	 *
	 */
	class BlinkHandler extends TimerTask {

		// number of times to blink
		private int count;
		
		public BlinkHandler( Timer timer, int count ) {
			this.count = count;
		}
		
		@Override
		public void run() {

			count--;
			
			if (count <= 0) {
				// stop blinking - stay highlighted
				blinkTimer.cancel();
				currentTextfieldColor = blinkColor;
				
			} else {
				if (currentTextfieldColor == blinkColor) {
					currentTextfieldColor = graveyardColor;
				} else {
					currentTextfieldColor = blinkColor;
				}
			}
			
			repaint();
		}
		
	}
}
